package com.joker.aigc.tools.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

/**
 * @Desciption:
 * @Author: feixiang.li
 * @date: 2024-08-05 14:11
 **/
public class FileUtils {


    public static String readFile(String filePath) {
        String content = "";
        try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)) {
            content = reader.lines().collect(Collectors.joining(System.lineSeparator()));
        } catch (IOException e) {
            throw new RuntimeException("Error reading SQL file", e);
        }
        return content;
    }

    /**
     * 读取文件里面的内容
     *
     * @param multipartFile 文件
     * @return 文件里面的字符串
     */
    public static String readFile(MultipartFile multipartFile) {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(multipartFile.getInputStream(), StandardCharsets.UTF_8))) {
            content.append(reader.lines().collect(Collectors.joining(System.lineSeparator())));
        } catch (IOException e) {
            throw new RuntimeException("Error reading SQL file", e);
        }
        return content.toString();
    }
}
